<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="NameSpace" prop="nameSpace">
        <el-input v-model.trim="form.nameSpace" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { AddCNameSpace } from '@/api/projectConfig/cNameSpace'

  export default {
    name: 'CNameSpaceEditForm',
    data() {
      return {
        form: {
          id: '',
          projectID: '',
          nameSpace: '',
        },
        rules: {
          nameSpace: [
            { required: true, trigger: 'blur', message: 'NameSpace不能为空' },
            {
              min: 1,
              max: 50,
              message: 'NameSpace长度在1到50个字符之间',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(projectId) {
        this.title = '添加NameSpace'
        this.form.projectID = projectId
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            AddCNameSpace(this.form).then((res) => {
              if (res.data) {
                this.$baseMessage(res.msg, 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          }
        })
      },
    },
  }
</script>
